package me.zhengjie.modules.system.rest

import io.swagger.v3.oas.annotations.media.Schema
import jakarta.servlet.http.HttpServletResponse
import me.zhengjie.common.exception.BadRequestException
import me.zhengjie.common.utils.*
import me.zhengjie.logging.annotation.Log
import me.zhengjie.modules.system.domain.Dept
import me.zhengjie.modules.system.service.DeptService
import me.zhengjie.modules.system.service.dto.DeptDTO
import me.zhengjie.modules.system.service.dto.DeptQueryCriteria
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import java.io.IOException

/**
 * @author Kuki Wu
 * @date 2019-03-25
 */
@RestController
@Schema(description = "系统：部门管理")
@RequestMapping("/api/dept")
class DeptController(private val deptService: DeptService) {
    @Log("导出部门数据")
    @Schema(description = "导出部门数据")
    @GetMapping(value = ["/download"])
    @PreAuthorize("@el.check('dept:list')")
    @Throws(
        IOException::class
    )
    fun download(response: HttpServletResponse?, criteria: DeptQueryCriteria?) {
        deptService.download(deptService.queryAll(criteria!!,false) as List<DeptDTO>, response)
    }

    @Log("查询部门")
    @Schema(description = "查询部门")
    @GetMapping
    @PreAuthorize("@el.check('user:list','dept:list')")
    @Throws(Exception::class)
    fun getDept(criteria: DeptQueryCriteria): ResponseEntity<PageResult<DeptDTO>>{
        // 数据权限
        val depts: List<DeptDTO> = deptService.queryAll(criteria, true) as List<DeptDTO>
        return  ResponseEntity(PageUtil.toPage(depts, depts.size.toLong()), HttpStatus.OK)
    }

    @Log("新增部门")
    @Schema(description = "新增部门")
    @PostMapping
    @PreAuthorize("@el.check('dept:add')")
    fun create(@Validated @RequestBody resources: Dept): ResponseEntity<*> {
        if (resources.id != null) {
            throw BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID")
        }
        return ResponseEntity(deptService.create(resources), HttpStatus.CREATED)
    }

    @Log("修改部门")
    @Schema(description = "修改部门")
    @PutMapping
    @PreAuthorize("@el.check('dept:edit')")
    fun update(
        @Validated(
            Dept.Update::class
        ) @RequestBody resources: Dept?
    ): ResponseEntity<*> {
        deptService.update(resources!!)
        return ResponseEntity<Any?>(HttpStatus.NO_CONTENT)
    }

    @Log("删除部门")
    @Schema(description = "删除部门")
    @DeleteMapping(value = ["/{id}"])
    @PreAuthorize("@el.check('dept:del')")
    fun delete(@PathVariable id: Long?): ResponseEntity<*> {
        try {
            deptService.delete(id!!)
        } catch (e: Throwable) {
            ThrowableUtil.throwForeignKeyException(e, "该部门存在岗位或者角色关联，请取消关联后再试")
        }
        return ResponseEntity<Any?>(HttpStatus.OK)
    }

    companion object {
        private const val ENTITY_NAME = "dept"
    }
}